package com.demo.domain;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;

public class ApiResponse<T> {

    private static ObjectMapper emptyMapper = new ObjectMapper();
    @ApiModelProperty(required = true, notes = "结果码", example = "S_OK")
    private String code;
    @ApiModelProperty(required = true, notes = "返回信息说明", example = "SUCCESS")
    private String message = "";
    @ApiModelProperty(required = true, notes = "返回数据")
    private JsonNode var;
    @ApiModelProperty(required = true, notes = "泛型类型的返回数据")
    private T result;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public JsonNode getVar() {
        return var;
    }

    public void setVar(JsonNode var) {
        this.var = var;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public static ApiResponse empty() {
        ApiResponse a = new ApiResponse();
        a.code = RetCode.S_OK.toString();
        a.message = "操作成功";
        return a;
    }

    public static ApiResponse s(String message) {
        ApiResponse a = new ApiResponse();
        a.code = RetCode.S_OK.toString();
        a.message = message;
        return a;
    }

    public static ApiResponse f(RetCode code, String message) {
        ApiResponse a = new ApiResponse();
        a.code = code.toString();
        a.message = message;
        return a;
    }

    public ApiResponse() {
    }

    public ApiResponse(String code, String message, JsonNode var) {
        this.code = code;
        this.message = message;
        this.var = var;
    }

    public ApiResponse(String code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public static ApiResponse emptyResponse(RetCode code) {
        ApiResponse r = new ApiResponse();
        r.setCode(code.toString());
        r.setVar(emptyMapper.createObjectNode());
        return r;
    }

    public ApiResponse(T result) {
        this.code = RetCode.S_OK.toString();
        this.message = "操作成功";
        this.result = result;
    }

    public ApiResponse(T result, String message) {
        this.code = RetCode.S_OK.toString();
        this.message = message;
        this.result = result;
    }

    public ApiResponse(RetCode code, String message, T result) {
        this.code = code.toString();
        this.message = message;
        this.result = result;
    }


    public enum RetCode {

        // 通用
        S_OK, // 执行成功
        F_USER_EMPTY, //
        F_VER_TOO_OLD, //
        F_UNKNOWN, // 未知错误
        F_NOT_FOUND, // 未定义命令
        F_SEQUENCIAL, //
        F_SID_INVALID, // sid过期
        F_DEPRECATED, //
        F_NOT_UNIQUE, //
        F_LIMIT, //
        F_FREQUENCY, // 频率异常
        F_FORBIDDEN, // 没有权限
        F_DUPLICATED, // 没有权限
        F_OVERLIMIT, //超出限制
        F_ILLEGAL_ARGUS,
        F_NEW_USER_WIN_GROUP_EXISTS,
        F_GUILD_LEADER_CAN_NOT_UPDATE,
        F_UID_GROUP_EXISTS,
        F_PARAMETER_ERROR, //参数错误
        F_NO_POWER,
    }


}